package org.example.myleet.p1356;

import java.util.Arrays;

class Solution {
    public int[] sortByBits(int[] arr) {
        Integer[] nums = new Integer[arr.length];
        for (int i=0; i<arr.length; i++) {
            nums[i] = arr[i];
        }
        Arrays.sort(nums, (o1, o2) -> {
            int o1BitCount = Integer.bitCount(o1);
            int o2BitCount = Integer.bitCount(o2);
            if (o1BitCount != o2BitCount) {
                return o1BitCount - o2BitCount;
            }
            return o1 - o2;
        });
        for (int i=0; i<nums.length; i++) {
            arr[i] = nums[i];
        }
        return arr;
    }
}
